<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\ForbiddenHttpException;
use yii\grid\GridView;
use yii\helpers\Html;
use backend\models\ShopExpense;
use yii\base\Object;
use yii\base\Response;
use yii\widgets\ActiveForm;
use backend\models\ShopExpenseSearch;
use yii\filters\VerbFilter;
use backend\models\GrpCity;
/**
 * 物流控制器
 * @author suiyue
 *
 */
class ExpenseController extends BaseController
{
	/**
	 * (non-PHPdoc)行为
	 * @see \yii\base\Component::behaviors()
	 */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * (non-PHPdoc)前置函数
     * @see \yii\web\Controller::beforeAction()
     */
    public function beforeAction($action)
    {
    	$action = Yii::$app->controller->action->id;
    	$controller = Yii::$app->controller->id;
    	$action_list = array(
    			'index',
    			'update',
    			'view',
    			'create',
    	        'addcity',
                'delete',
    	);
    	 $access=$this->checkAccess($action_list,$action);//判断操作的action是否在actionlist里面
    	 $this->beforeOp('brand', $access);//身份识别 权限验证  action验证
    	 return true;
    }
	/**
	 * 物流列表
	 */
    public function actionIndex()
    {
        
        $model = new ShopExpenseSearch();
        $list=$model->search(Yii::$app->request->queryParams);
        //查询brand_head列表
        return $this->render('index', [
            'list' => $list,
            'model'=>$model,
        ]);
    }
    
    
    /**
     * 创建物流
     */
    public function actionCreate(){
        if (Yii::$app->request->isPost){
            $list = Yii::$app->request->post();
            $objectPost = new ShopExpense();        
            $objectPost->shop_id = 1; 
            $objectPost->name = $list['name'];
            $objectPost->if_postage = $list['if_postage'];
            $objectPost->count_type = $list['count_type'];
            $objectPost->type = $list['type'];
            if($list['child']){
                $objectPost->content = json_encode($list['child']);
            }
            $objectPost->createtime = time();
            if($objectPost->save()){
                return json_encode([
                  'code' => 1,
                  'msg' => '添加成功',
                 ]);
            }else{ 
                 return json_encode([
                  'code' => 2,
                  'msg' => '添加失败',
                 ]); 
            }
            
        }
        $model = new ShopExpense();
        //查询城市列表
        //查询所有省份
        $sql="  SELECT id,name  FROM grp_city WHERE id%10000=0 ";
        $provinceList=GrpCity::findBySql($sql)->asArray()->all();
        //设置为键值对  省份为键 城市为市   直辖市另行处理
        for($i=0;$i<count($provinceList);$i++){
            $provinceId=$provinceList[$i]['id'];
            $specilProvince=array('110000','120000','310000','500000','710000','810000','820000');
            $firstId=substr($provinceId,0,2);
            if(in_array($provinceId, $specilProvince)){
                $sql="select id,name from grp_city where id like '{$firstId}%' and id !=:id";
            }else{
                $sql="select id,name from grp_city where id like '{$firstId}%00' and id!=:id";
            }
            $param=array(":id"=>$provinceId);
            $cityList=GrpCity::findBySql($sql,$param)->asArray()->all();
            $provinceList[$i]['childlist']=$cityList;
        }
        return $this->render('create',[
            'url'=>Yii::$app->request->get('url'),
            'model'=>$model,
            'list'=>$provinceList,
        ]);
    }
    /**
     * 添加城市
     * @return string|\yii\web\Response
     */
    public function actionAddcity(){
        if (Yii::$app->request->isGet)
        {
            //查询城市列表
            //查询所有省份
            $sql="  SELECT id,name  FROM grp_city WHERE id%10000=0 ";
            $provinceList=GrpCity::findBySql($sql)->asArray()->all();
            //设置为键值对  省份为键 城市为市   直辖市另行处理
            for($i=0;$i<count($provinceList);$i++){
                $provinceId=$provinceList[$i]['id'];
                $specilProvince=array('110000','120000','310000','500000','710000','810000','820000');
                $firstId=substr($provinceId,0,2);
                if(in_array($provinceId, $specilProvince)){
                    $sql="select id,name from grp_city where id like '{$firstId}%' and id !=:id";
                }else{
                    $sql="select id,name from grp_city where id like '{$firstId}%00' and id!=:id";
                }
                $param=array(":id"=>$provinceId);
                $cityList=GrpCity::findBySql($sql,$param)->asArray()->all();
                $provinceList[$i]['childlist']=$cityList;
            }
            return $this->renderAjax('_form_add_city',[
                'url'=>Yii::$app->request->get('url'),
                'model'=>$provinceList,
            ]);
            //页面点击确定，处理好选择的城市，跳转页面  
        } elseif (Yii::$app->request->isPost){
            $model='';
            if ($model->load(Yii::$app->request->post()))
            {
                return $this->redirect(Yii::$app->request->post('url'));
            }
            
        }
    }
    /**
     * 修改
     */
    public function actionUpdate(){
    	if (Yii::$app->request->isPost){
            $list = Yii::$app->request->post();
            $objectPost=ShopExpense::findOne($list['id']);     
            $objectPost->name = $list['name'];
            $objectPost->if_postage = $list['if_postage'];
            $objectPost->count_type = $list['count_type'];
            $objectPost->type = $list['type'];
            if($list['child']){
                $objectPost->content = json_encode($list['child']);
            }else{
                $objectPost->content = null;
            }
            $objectPost->updatetime = time();
            if($objectPost->update()){
                return json_encode([
                  'code' => 1,
                  'msg' => '更新成功',
                 ]);
            }else{ 
                 return json_encode([
                  'code' => 2,
                  'msg' => '更新失败',
                 ]); 
            }
            
        }else{
            $id=Yii::$app->request->get('id');
            $model=ShopExpenseSearch::findOne($id);
            $content = json_decode($model['content'],true);
            //查询城市列表
            //查询所有省份
            $sql="  SELECT id,name  FROM grp_city WHERE id%10000=0 ";
            $provinceList=GrpCity::findBySql($sql)->asArray()->all();
            //设置为键值对  省份为键 城市为市   直辖市另行处理
            for($i=0;$i<count($provinceList);$i++){
                $provinceId=$provinceList[$i]['id'];
                $specilProvince=array('110000','120000','310000','500000','710000','810000','820000');
                $firstId=substr($provinceId,0,2);
                if(in_array($provinceId, $specilProvince)){
                    $sql="select id,name from grp_city where id like '{$firstId}%' and id !=:id";
                }else{
                    $sql="select id,name from grp_city where id like '{$firstId}%00' and id!=:id";
                }
                $param=array(":id"=>$provinceId);
                $cityList=GrpCity::findBySql($sql,$param)->asArray()->all();
                $provinceList[$i]['childlist']=$cityList;
            }
            return $this->render('update',[
                'url'=>Yii::$app->request->get('url'),
                'model'=>$model,
                'list'=>$provinceList,
                'content'=>$content,
            ]);
        }
    }
    
    /**
     * 查看单条数据
     */
    public function actionView($id){
    	$this->layout = 'main_small';
    	$model=ShopExpenseSearch::findOne($id);
        $content = json_decode($model['content'],true);
    	return $this->render('view', [
    			'model' => $model,
                'content'=>$content,
    	]);
    }
    /**
     * 删除单条数据
     */
     public function actionDelete($id){
        ShopExpense::findOne($id)->delete();
        return $this->redirect(array('expense/index'));
    }
    /**
     * 查找单个
     * @param   $id
     * @throws NotFoundHttpException
     */
    protected function findModel($id)
    {
        if (($model = Brand::findOne($id)) !== null) {
        	$model = Brand::find()->where('id='.$id)->one();
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
   
    
}
